package xin.marcher.module.quality.cor.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.marcher.module.common.utils.LocalDateTimeUtil;
import xin.marcher.module.quality.cor.RiskHandler;
import xin.marcher.module.quality.domain.dto.ItemQualityControlResultDTO;
import xin.marcher.module.quality.domain.entity.ItemQualityControlSurveyTaskDO;
import xin.marcher.module.quality.repository.ItemQualityControlRepository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 */
@Component
public class SurveyTaskPoolHandler implements RiskHandler {

    @Autowired
    private ItemQualityControlRepository itemQualityControlRepository;

    /**
     * 待处理状态
     */
    private final static int PENDING = 1;

    /**
     * 未通过状态
     */
    private final static int NOT_PASS = 2;

    /**
     * 已通过状态
     */
    private final static int PASSED = 3;

    /**
     * 继续上传资料状态
     */
    private final static int ADDITIONAL = 4;

    /**
     * 未通过状态7天内禁止重复检查，这个常数是保护周期
     */
    private final static int NOT_PASS_PROTECT_PERIOD = 7;

    /**
     * 处理任务集中已存在的风险数据
     *
     * @param riskDataMap
     * @return
     */
    @Override
    public Map<String, ItemQualityControlResultDTO> handle(Map<String, ItemQualityControlResultDTO> riskDataMap) {
        // 风险ID集合
        Set<String> surveyIdSet = riskDataMap.keySet();
        // 已存在的风险数据
        List<ItemQualityControlSurveyTaskDO> existsInSurveyTaskDataList = itemQualityControlRepository.queryExistsInSurveyTaskData(surveyIdSet);

        // 保存未处理的数据集合
        List<ItemQualityControlSurveyTaskDO> pendingTaskList = new ArrayList<>();
        // 保存未通过的数据集合
        List<ItemQualityControlSurveyTaskDO> notPassTaskList = new ArrayList<>();
        // 保存继续上传资料的数据集合
        List<ItemQualityControlSurveyTaskDO> additionalTaskList = new ArrayList<>();

        for (ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO : existsInSurveyTaskDataList) {
            switch (itemQualityControlSurveyTaskDO.getAuditStatus()) {
                // 筛选出状态为待处理的
                case PENDING:
                    pendingTaskList.add(itemQualityControlSurveyTaskDO);
                    break;
                // 筛选出状态为未通过的
                case NOT_PASS:
                    notPassTaskList.add(itemQualityControlSurveyTaskDO);
                    break;
                // 筛选出状态为继续上传证明
                case ADDITIONAL:
                    additionalTaskList.add(itemQualityControlSurveyTaskDO);
                    break;
            }
        }

        // 待处理状态，更新风险等级，并剔除
        itemQualityControlRepository.updateExistsInSurveyTaskData(riskDataMap, pendingTaskList);

        // 继续上传证明状态的，直接剔除
        for (ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO : additionalTaskList) {
            riskDataMap.remove(itemQualityControlSurveyTaskDO.getSurveyId());
        }

        // 剔除掉未通过状态，并且还没有超过保护时间的
        for (ItemQualityControlSurveyTaskDO itemQualityControlSurveyTaskDO : notPassTaskList) {
            // 现在的时间
            LocalDateTime now = LocalDateTime.now();
            // 上次更新的时间
            LocalDateTime lastUpdateTime = LocalDateTimeUtil.dateToLocalDateTime(itemQualityControlSurveyTaskDO.getUpdateTime());
            // 上次更新的时间加上保护时间
            LocalDateTime plusUpdateTime = lastUpdateTime.plusDays(NOT_PASS_PROTECT_PERIOD);

            // 剔除掉没有超过保护时间的数据
            if (now.isAfter(plusUpdateTime)) {
                riskDataMap.remove(itemQualityControlSurveyTaskDO.getSurveyId());
            }
        }

        return riskDataMap;
    }
}
